<?php
Yii::import('ext.twitter.lib.EpiCurl');
Yii::import('ext.twitter.lib.EpiOAuth');
Yii::import('ext.twitter.lib.EpiTwitter');


class ETwitter extends CApplicationComponent {

	public $consumerKey;
	public $consumerSecret;
	public $userToken;
	public $userSecret;
	public $source='Website';
	public $debug=false;

	public function setStatus($text){
		if($this->debug){
			return  true;
		}
		try{
			//$t=new Twitter($this->username,$this->password,$this->source);
			$twitterObj = new EpiTwitter($this->consumerKey,$this->consumerSecret,
			$this->userToken,$this->userSecret);

				
			$pattern='@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@';//[(([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*))\]/";
			$text=preg_replace_callback($pattern,array( &$this, 'convertToTiny'), $text);
			//$t->updateStatus($text);
			$x = $twitterObj->post_statusesUpdate(array('status' => $text));
//	if ($x->id) {
//		echo "Tweet Posted: ".$x->id."\n";
//	} else {
//		echo "Error Posting Tweet\n";
//	}
			return true;
		}catch(Exception $e){
			Yii::log($e->getTraceAsString(), CLogger::LEVEL_ERROR);
		}
	}

	public function convertToTiny($matches){
		return $this->tinyURL($matches[0]);
	}

	public function tinyURL($u){
		return file_get_contents('http://tinyurl.com/api-create.php?url='.$u);
	}
}